package com.ant.myagile

import com.ant.myagile.utils.CommonUtils;


class Member {

	transient springSecurityService

	String firstName
	String lastName
	String gender
	String skype
	String mobile
	String avatar
	String company
	String username
	boolean isActive = false;
	String token = CommonUtils.randomToken();
	String tokenPass = CommonUtils.randomToken();
	Date tokenDate = new Date();
	String password
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired

	static constraints = {
		username blank: false, unique: true, email:true
		 firstName nullable:true
		 lastName nullable:true
		 gender nullable:true
		 skype nullable:true
		 mobile nullable:true
		 avatar nullable:true 
		 company nullable:true		
		 password nullable:true
	}
	
	static hasMany = [teamMembers: Team_Member, projects : Project, team: Team]
	
	static mapping = {
		password column: '`password`'
	}

	Set<Role> getAuthorities() {
		MemberRole.findAllByMember(this).collect { it.role } as Set
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
		
	def String toString(){
		return firstName+" "+lastName
	}
	
}
